package com.example.demo.controller;

import com.example.demo.VO.Result;
import com.example.demo.entity.Judgment;
import com.example.demo.entity.Short1;
import com.example.demo.service.Short1Service;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Short1)表控制层
 *
 * @author hjl
 * @since 2020-11-30 19:33:04
 */
@RestController
@RequestMapping("short1")
@Api(value = "简答题", tags = {"简答题操作接口"})
public class Short1Controller {
    /**
     * 服务对象
     */
    @Autowired
    private Short1Service short1Service;

    @GetMapping("selectOne")
    public Short1 selectOne(Integer id) {
        return this.short1Service.queryById(id);
    }

    /**
     * 根据属性查询 （当参数为空时候）查询所有
     *
     * @param short1
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Short1>> query(@RequestBody(required = false) Short1 short1) {
        return ResponseEntity.ok(short1Service.queryByExample(short1));
    }

    /**
     * 插入一条数据
     *
     * @param short1
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<List<Short1>> add(@RequestBody Short1 short1) {
        Short1 short11 = new Short1();
        if (short1.getId() != null) {
            short1Service.update(short1);
        } else {
            short1Service.insert(short1);
        }

        return ResponseEntity.ok(short1Service.queryByExample(new Short1()));
    }

    /**
     * 根据主键更新数据
     *
     * @param short1
     * @return
     */
    @PutMapping("update")
    public ResponseEntity<List<Short1>> update(@RequestBody Short1 short1) {
        short1Service.update(short1);
        return ResponseEntity.ok(short1Service.queryByExample(new Short1()));
    }

    /**
     * 根据主键删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("del/{id}")
    public ResponseEntity<List<Short1>> delete(@PathVariable("id") Integer id) {
        short1Service.deleteById(id);
        return ResponseEntity.ok(short1Service.queryByExample(new Short1()));
    }

    @PostMapping("delAll")
    public ResponseEntity<List<Short1>> deleteAll(@RequestBody Integer[] id) {
        for (int i = 0; i < id.length; i++) {
            short1Service.deleteById(id[i]);
        }
        return ResponseEntity.ok(short1Service.queryByExample(new Short1()));
    }

    @PostMapping("findShort1ByCourseid")
    public Result findShort1ByCourseid(Integer courseId) {
        Short1 short1 = new Short1();
        short1.setObj2(courseId + "");
        return Result.success(short1Service.queryByExample(short1));
    }

}